﻿using EmployeeCard.Api.Filtter;
using EmployeeCard.Common;
using EmployeeCard.Common.Helper;
using EmployeeCard.Common.Redis;
using EmployeeCard.Common.Redis.Interface;
using EmployeeCard.IServices;
using EmployeeCard.Model.Models;
using EmployeeCard.Services;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace EmployeeCard.Api.Controllers
{
    /// <summary>
    /// 博客控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [Authorize(Policy = "Admin")]
    public class BlogArticleController : ControllerBase
    {
        IBlogArticleServices _articleServices;
        IRedisBasketRepository _redisBasketRepository;
        public BlogArticleController(IBlogArticleServices articleServices, IRedisBasketRepository redisBasketRepository)
        {
            _articleServices = articleServices;
            _redisBasketRepository = redisBasketRepository;
        }

        /// <summary>
        /// 获取博客列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("Blog")]
        public async Task<IActionResult> GetBlogs(int id, int page = 1, string bcategory = "技术")
        {
            //return await _articleServices.GetBlogs();
            int intTotalCount = 6;
            int TotalCount = 1;
            List<BlogArticle> blogArticleList = new List<BlogArticle>();

            if (await _redisBasketRepository.Get<object>("Redis.Blog") != null)
            {
                blogArticleList = await _redisBasketRepository.Get<List<BlogArticle>>("Redis.Blog");
            }
            else
            {
                blogArticleList = await _articleServices.Query(a => a.bcategory == bcategory);
                await _redisBasketRepository.Set("Redis.Blog", blogArticleList, TimeSpan.FromHours(2));
            }


            TotalCount = blogArticleList.Count() / intTotalCount;

            blogArticleList = blogArticleList.OrderByDescending(d => d.bID).Skip((page - 1) * intTotalCount).Take(intTotalCount).ToList();

            foreach (var item in blogArticleList)
            {
                if (!string.IsNullOrEmpty(item.bcontent))
                {
                    int totalLength = 500;
                    if (item.bcontent.Length > totalLength)
                    {
                        item.bcontent = item.bcontent.Substring(0, totalLength);
                    }
                }
            }

            var data = new { success = true, page = page, pageCount = TotalCount, data = blogArticleList };


            return Ok(data);
        }

        /// <summary>
        /// 获取单个博客详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetBlog/{id}")]
        public async Task<object> GetBlog(int id)
        {
            var blogArticle = await _redisBasketRepository.Get<BlogArticle>($"Redis.Blog.{id}");
            if (blogArticle != null)
            {
                return blogArticle;
            }
            blogArticle = await _articleServices.QueryByID(id);
            await _redisBasketRepository.Set($"Redis.Blog.{id}", blogArticle, TimeSpan.FromSeconds(3000));
            return blogArticle;
        }

        /// <summary>
        /// 获取博客详情-模型映射
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetBlogDetails/{id}")]
        public async Task<object> GetBlogDetails(int id)
        {
            var blogViewModel = await _articleServices.GetBlogDetails(id);
            return blogViewModel;
        }
    }
}
